﻿<?php



class PartnerAction extends CommonAction {


    public function edit() {
        
        if ($this->_get('id')) {
            $m = M('Partner');
            $condition['id'] = $this->_get('id');
            $res = $m->where($condition)->select();
            
            if($res){
                $this->assign('vo', $res[0]); 
            }
        }else{
            $this->ajaxReturn(0, '非法进入', 0);
        }


        $this->display();
    }
    
    
    public function update(){
        

        if($_POST['subflag'] && $_POST['ok']){
            $id = $this->_post('id');
            $data['name'] = $this->_post('name');
            $data['status'] = $this->_post('status');
            $img_dir="./Public/Uploads/";
            //上传图片
            if($_FILES["logo"]["name"]){
                    $data['logo'] = $this->img_upload('logo',60000*1024,$img_dir,'200','200');
            }
          
            if($data['logo']){
                $sql = "update ciwen_partner set name='".$data['name']."' ,logo='".$data['logo']."' ,status=".$data['status']." where id=".$id;
            }else{
                 $sql = "update ciwen_partner set name='".$data['name']."' ,status=".$data['status']." where id=".$id;
            }
            $Model = new Model(); 
            $result = $Model->execute($sql); 
            if($result) {
               $this->success('操作成功！',"index");
            }else{
               $this->error('写入错误！');
            }
        }else{
            $this->success('',"index");
        }
        
    }

    
    
    public function add(){
        
        if($_POST['subflag']){

                $data['logo'] = $this->_post['logo'];
                $data['name'] = $this->_post('name');
                $data['status'] = $this->_post('status');

                $sql = "insert into ciwen_partner set name='".$data['name']."' ,logo='".$data['logo']."' ,status=".$data['status'];
                $Model = new Model(); 
                $result = $Model->execute($sql); 
                if($result) {
                    $this->success('操作成功！',"index");
                    exit();
                }else{
                    $this->error('写入错误！');
                    exit();
                }

            }
            $this->assign('type', 'add'); 
            $this->display("edit");
    }
    
    
    //接收上传图片
	public function uploadimg()
	{

		import('ORG.Net.UploadFile');
		
		$config = array(
			'maxSize'           =>  1048576,    // 上传文件的最大值 1mb
			'allowExts'         =>  array('jpg','jpeg','png','gif'),    // 允许上传的文件后缀 留空不作后缀检查
			'savePath'          =>  'Public/Uploads/partner/',// 上传文件保存路径
			'autoSub'			=>	true,
			'subType'			=>	'unique',
        );

		$f = new UploadFile($config);
		if( $f->upload($_FILES['file_upload']) )
		{
			$res = $f->getUploadFileInfo();
			$imgdir = C('BASE_PATH').'/Public/Uploads/partner/'.$res[0]['savename'];
			$thumb_name = getimg( $imgdir,49,30);

			if($thumb_name !== false)
			{
				$this->ajaxReturn( array('status'=>1,'imgname'=>str_replace('/Public/Uploads/partner','',$thumb_name)) );
			}else{
				$this->ajaxReturn( array('status'=>0,'error'=>'缩略图生成出错 ' ) );
			}
		}else{
			$this->ajaxReturn( array('status'=>0,'error'=>$f->getErrorMsg() ) );
		}
	}

	//删除图片
	public function delimg()
	{
		$img = $this->_get('img');
		
		$deldir = substr($img,0,18);	//得到目录名, 把这个目录都删了

		echo C('BASE_PATH').'/Public/Uploads/partner'.$deldir;

		unlink( C('BASE_PATH').'/Public/Uploads/partner'.$deldir );
	}
    
   
}